#============================================================
#	Equipment
#============================================================
# @datetime: 2022-4-20 23:16:27
#============================================================
class_name HUD_Equipment
extends PanelContainer


signal use_item(item_data)


const ICON = preload("res://icon.png")



onready var _item_icon := get_node("MarginContainer/VBoxContainer/HBoxContainer/ItemIcon") as TextureRect
onready var _item_label := get_node("MarginContainer/VBoxContainer/HBoxContainer/ItemLabel") as Label
onready var _use_button := get_node("MarginContainer/VBoxContainer/VBoxContainer/HBoxContainer2/UseButton") as Button
onready var _description := get_node("MarginContainer/VBoxContainer/VBoxContainer/PanelContainer/MarginContainer/ScrollContainer/VBoxContainer/Description") as RichTextLabel


# 当前显示的物品
var _item : Dictionary


#============================================================
#   内置
#============================================================
func _ready():
	show_item({})


#============================================================
#   自定义
#============================================================
func show_item(data: Dictionary):
	if _item.hash() == data.hash():
		return
	_item = data
	_update_use_button(data)
	if _item.has("icon"):
		if _item['icon'] is String:
			_item_icon.texture = load(_item['icon']) 
		elif _item['icon'] is Texture:
			_item_icon.texture = _item['icon']
			
	else:
		_item_icon.texture = ICON
	_item_label.text = data.get("name", "ItemName")
	
#	_description.bbcode_text = data.get("desc", "Description")


# 更新按钮示
func _update_use_button(data: Dictionary):
	if data.has("count") and data['count'] > 0:
		_use_button.text = "使用(%d)" % data['count']
	else:
		_use_button.text = "使用"
	if data.has('count'):
		_use_button.disabled = (data['count'] <= 0)
	else:
		_use_button.disabled = (data.size() == 0)
	
	if data.size() == 0:
		show_item({})


#============================================================
#   连接信号
#============================================================
func _on_Use_pressed():
	# 装载物品
	emit_signal("use_item", _item)
	_update_use_button(_item)


